<?php

namespace App\Observers;

use App\Models\Constant\OrderConstant;
use App\Models\Order\Order;
use App\Models\Repair\RepairFactoryAdmin;
use App\Models\Setting\SysSetting;
use App\Models\User\User;
use EasyWeChat\Factory;
use Illuminate\Support\Facades\Log;

class OrderObserver
{

    public function saved(Order $order) {
        Log::info('saved',$order->getAttributes());
        $user = User::where('id',$order->user_id)->first();
        $wx_public_open_id = $user->wx_public_open_id??null;
        $config = config('wechat.public');
        $app = Factory::officialAccount($config);
        $templateId = 'Vn9XxWQZcK8dBuVqbT2NbLyNCoXgT9CCSSr309NFMAE';
        //申请成功，发送通知给当前用户
        if(in_array($order->getOriginal('status'), [OrderConstant::STATUS_CHECK_NOT_PASS,OrderConstant::STATUS_WAIT_AUTO_CHECK])
            &&  $order->getAttribute('status')==OrderConstant::STATUS_WAIT_HUMAN_CHECK){
            //发送短信
            if(!empty($user)){
                $template= 'SMS_253240330';
                $verifyCode =0;
                sendSms($user->phone,$template,$verifyCode);
            }else{
                Log::info('order:STATUS_CHECK_PASS==>id'.$order->id.'未找到有效用户');
            }

            //发送微信消息
            if(!empty($wx_public_open_id)){
                $app->template_message->send([
                    'touser' => $wx_public_open_id,
                    'template_id' => $templateId ,
                    'miniprogram' => [
                        'appid' => env('WX_SMALL_APPID'),
                        'pagepath' => 'pages/index/index',
                    ],
                    'data' => [
                        'first' => '您好，您的申请已提交，我们会尽快审核，请耐心等待。',
                        'keyword1' => '保驾优赔',
                        'keyword2' => '待审核',
                        'keyword3' => date('Y-m-d H:i:s'),
                        'remark' => '详情请登录小程序查看',
                    ],
                ]);
            }

        }

        //申请 审批通过,状态变成待报销发送通知给用户
        if($order->getAttribute('status')==OrderConstant::STATUS_WAIT_REPAY){
            //发送短信
            if(!empty($user)){
                $phone = $user->phone;
                $template= 'SMS_253080361';
                $verifyCode =0;
                sendSms($phone,$template,$verifyCode);
            }else{
                Log::info('order:STATUS_CHECK_PASS==>id'.$order->id.'未找到有效用户');
            }

            //发送微信消息
            if(!empty($wx_public_open_id)){
                $app->template_message->send([
                    'touser' => $wx_public_open_id,
                    'template_id' => $templateId ,
                    'miniprogram' => [
                        'appid' => env('WX_SMALL_APPID'),
                        'pagepath' => 'pages/index/index',
                    ],
                    'data' => [
                        'first' => '您好，您的申请已审核通过，我们将在2个工作日内完成打款，请注意查收',
                        'keyword1' => '保驾优赔',
                        'keyword2' => '已通过',
                        'keyword3' => date('Y-m-d H:i:s'),
                        'remark' => '详情请登录小程序查看',
                    ],
                ]);
            }
        }

        //审核不通过，发送短信
        if($order->getAttribute('status')==OrderConstant::STATUS_CHECK_NOT_PASS){
            if(!empty($user)){
                $phone = $user->phone;
                $template= 'SMS_252995357';
                $verifyCode =0;
                sendSms($phone,$template,$verifyCode);
            }else{
                Log::info('order:STATUS_CHECK_NOT_PASS==>id'.$order->id.'未找到有效用户');
            }

            //发送微信消息
            if(!empty($wx_public_open_id)){
                $app->template_message->send([
                    'touser' => $wx_public_open_id,
                    'template_id' => $templateId ,
                    'miniprogram' => [
                        'appid' => env('WX_SMALL_APPID'),
                        'pagepath' => 'pages/index/index',
                    ],
                    'data' => [
                        'first' => '您好，您的申请未通过审核',
                        'keyword1' => '保驾优赔',
                        'keyword2' => '未通过',
                        'keyword3' => date('Y-m-d H:i:s'),
                        'remark' => '详情请登录小程序查看',
                    ],
                ]);
            }
        }

        //报销完了，订单结束
        if($order->getAttribute('status')==OrderConstant::STATUS_END){

            if(!empty($user)){
                $phone = $user->phone;
                $template= 'SMS_253030370';
                $verifyCode =0;
                sendSms($phone,$template,$verifyCode);
            }else{
                Log::info('order:STATUS_CHECK_NOT_PASS==>id'.$order->id.'未找到有效用户');
            }

            //发送微信消息
            if(!empty($wx_public_open_id)){
                $app->template_message->send([
                    'touser' => $wx_public_open_id,
                    'template_id' => $templateId ,
                    'miniprogram' => [
                        'appid' => env('WX_SMALL_APPID'),
                        'pagepath' => 'pages/index/index',
                    ],
                    'data' => [
                        'first' => '您好，您的申请已完成，请及时查收。感谢您使用保驾优赔，祝您生活愉快。',
                        'keyword1' => '保驾优赔',
                        'keyword2' => '已报销',
                        'keyword3' => date('Y-m-d H:i:s'),
                        'remark' => '详情请登录小程序查看',
                    ],
                ]);
            }
        }

        //取消订单
        if($order->getAttribute('status')==OrderConstant::STATUS_CANCEL){


            if(!empty($user)){
                $phone = $user->phone;
                $template= 'SMS_253240331';
                $verifyCode =0;
                sendSms($phone,$template,$verifyCode);
            }else{
                Log::info('order:STATUS_CHECK_NOT_PASS==>id'.$order->id.'未找到有效用户');
            }

            //发送微信消息
            if(!empty($wx_public_open_id)){
                $app->template_message->send([
                    'touser' => $wx_public_open_id,
                    'template_id' => $templateId ,
                    'miniprogram' => [
                        'appid' => env('WX_SMALL_APPID'),
                        'pagepath' => 'pages/index/index',
                    ],
                    'data' => [
                        'first' => '您好 您的申请已取消',
                        'keyword1' => '保驾优赔',
                        'keyword2' => '已取消',
                        'keyword3' => date('Y-m-d H:i:s'),
                        'remark' => '详情请登录小程序查看',
                    ],
                ]);
            }
        }

        //====================================================================================================


        //废弃 - 当状态改为待接单，发送短信给 工厂管理员
        if($order->getAttribute('status')==OrderConstant::STATUS_WAIT_RECEIVING){
            $phones = RepairFactoryAdmin::where('factory_id',$order->repair_factory_id)->pluck('phone');
            foreach ($phones as $phone) {
                $template= 'SMS_251710563';
                $verifyCode =0;
                sendSms($phone,$template,$verifyCode);
            }
        }

    }


    /**
     * Handle the Order "created" event.
     *
     * @param  \App\Models\Order\Order  $order
     * @return void
     */
    public function created(Order $order) {

        $user = User::where('id',$order->user_id)->first();
        $wx_public_open_id = $user->wx_public_open_id??null;
        $config = config('wechat.public');
        $app = Factory::officialAccount($config);
        $templateId = 'Vn9XxWQZcK8dBuVqbT2NbLyNCoXgT9CCSSr309NFMAE';
        //申请成功，发送通知给当前用户
        /*if(in_array($order->getOriginal('status'), [OrderConstant::STATUS_CHECK_NOT_PASS,OrderConstant::STATUS_WAIT_AUTO_CHECK])
            &&  $order->getAttribute('status')==OrderConstant::STATUS_WAIT_HUMAN_CHECK){*/
        if($order->getAttribute('status')==OrderConstant::STATUS_WAIT_HUMAN_CHECK){
            //发送短信
            if(!empty($user)){
                $template= 'SMS_253240330';
                $verifyCode =0;
                sendSms($user->phone,$template,$verifyCode);
            }else{
                Log::info('order:STATUS_CHECK_PASS==>id'.$order->id.'未找到有效用户');
            }

            //发送微信消息
            if(!empty($wx_public_open_id)){
                $app->template_message->send([
                    'touser' => $wx_public_open_id,
                    'template_id' => $templateId ,
                    'miniprogram' => [
                        'appid' => env('WX_SMALL_APPID'),
                        'pagepath' => 'pages/index/index',
                    ],
                    'data' => [
                        'first' => '您好，您的申请已提交，我们会尽快审核，请耐心等待。',
                        'keyword1' => '保驾优赔',
                        'keyword2' => '待审核',
                        'keyword3' => date('Y-m-d H:i:s'),
                        'remark' => '详情请登录小程序查看',
                    ],
                ]);
            }

            //发送消息给管理员
            $adminPhones = SysSetting::where('key','admin')->first()->val;
            $adminPhones = str_replace('，', ',', $adminPhones);
            Log::info('adminPhones',[$adminPhones]);
            $admins = User::whereIn('phone',explode(',', $adminPhones))->pluck('wx_public_open_id')->toArray();
            foreach ($admins as $admin) {
                $app->template_message->send([
                    'touser' => $admin,
                    'template_id' => $templateId ,
                    'miniprogram' => [
                        'appid' => env('WX_SMALL_APPID'),
                        'pagepath' => 'pages/index/index',
                    ],
                    'data' => [
                        'first' => '有新的申请，请及时审核',
                        'keyword1' => '保驾优赔',
                        'keyword2' => '待审核',
                        'keyword3' => date('Y-m-d H:i:s'),
                        'remark' => '详情请登录小程序查看',
                    ],
                ]);
            }

        }
    }

    /**
     * Handle the Order "updated" event.
     *
     * @param  \App\Models\Order\Order  $order
     * @return void
     */
    public function updated(Order $order) {

    }

    /**
     * Handle the Order "deleted" event.
     *
     * @param  \App\Models\Order\Order  $order
     * @return void
     */
    public function deleted(Order $order)
    {
        //
    }

    /**
     * Handle the Order "restored" event.
     *
     * @param  \App\Models\Order\Order  $order
     * @return void
     */
    public function restored(Order $order)
    {
        //
    }

    /**
     * Handle the Order "force deleted" event.
     *
     * @param  \App\Models\Order\Order  $order
     * @return void
     */
    public function forceDeleted(Order $order)
    {
        //
    }
}
